package CutLoad.web.slide
{
	import CutLoad.Base;
	import CutLoad.cookies.CLoadImg;
	import CutLoad.cookies.CLoader;
	import CutLoad.cookies.Fun;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * 幻灯片背景
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 上午10:00:20 
	 *
	 */
	public class CSlideBase extends Base
	{
		/**
		 * 存放滚动内容的容器 
		 */		
		protected var containerSlideItem:Sprite;
		/**
		 * 地址 
		 */		
		protected var arrData:Array;
		/**
		 * 当前播放头 
		 */		
		protected var current_index:Number;
		/**
		 * 当前显示的对象 
		 */		
		protected var currentDisplay:Base;
		/**
		 * 初始化 
		 * @param arrData 地址列表
		 * 
		 */		
		public function CSlideBase(arrData:Array)
		{
			this.arrData=arrData;	
			current_index=0;
			containerSlideItem=new Sprite();
			this.addChild(containerSlideItem);
		}
		/**
		 * 如果要加载一个swf，从这里获得一个加载swf的对象，进行处理 
		 * @param url
		 * @return 
		 * 
		 */		
		protected function getLoadSwf(url:String):Base{
			return new CLoader(url);	
		}
		/**
		 * 如果要加载一个图像，从这里获得一个加载图像的对象，进行处理 
		 * @param url
		 * @return 
		 * 
		 */		
		protected function getLoadImg(url:String):Base{
			return new CLoadImg(url);
		}
		/**
		 * 加载并显示某一个对像 
		 * @param index
		 * @return 
		 * 
		 */		
		public function showOne(index:uint):Boolean{
			if(index<0||index>arrData.length-1) return false;
			current_index=index;
			showCurrentIndex()
			return true;
		}
		public function next(isLoop:Boolean=true):Boolean{
			current_index++;
			if(isLoop){
				if(current_index>arrData.length-1) current_index=0
			}else{
				if(current_index>arrData.length-1) current_index=arrData.length-1
			}
			showCurrentIndex()
			return true;	
		}
		public function previous(isLoop:Boolean=true):Boolean{
			current_index--;
			if(isLoop){
				if(current_index<0) current_index=arrData.length-1
			}else{
				if(current_index<0) current_index=0
			}
			showCurrentIndex();
			return true;
		}
		override public function killMe():void{
			//去除当前的
			if(currentDisplay!=null){
				currentDisplay.killMe();
			}
			super.killMe();
		}
		/**
		 * 显示当前播放头设置的显示对象 
		 * 
		 */		
		protected function showCurrentIndex():void{
			var url:String=this.arrData[current_index];
			var suffix:String=Fun.getFileTypeName(url);
			//去除之前的
			if(currentDisplay!=null){
				currentDisplay.killMe();
			}
			//
			if(suffix=="swf"){
				currentDisplay=getLoadSwf(url);
			}else{
				//如果不是swf就是图片
				currentDisplay=getLoadImg(url);
			}
			containerSlideItem.addChild(currentDisplay);
		}
	}
}